<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-biz__roomrecord}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('biz:roomrecord:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('biz:roomrecord:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="location" label="民宿" header-align="center" align="center"></el-table-column>
        <el-table-column prop="roomName" label="房间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="dictName" label="类型" header-align="center" align="center"></el-table-column>
        <el-table-column prop="startHireDate" label="到店时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="dayMoney" label="价格" header-align="center" align="center"></el-table-column>
         <el-table-column prop="inDay" label="住宿天数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="totalMoney" label="总金额" header-align="center" align="center"></el-table-column>
         <el-table-column prop="endHireDate" label="离店时间" header-align="center" align="center"></el-table-column>
         <el-table-column prop="outMoney" label="应退金额" header-align="center" align="center"></el-table-column>
        <el-table-column prop="creatorStr" label="创建者" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">

            <el-button v-if="$hasPermission('biz:roomrecord:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
            <el-button type="text" size="small" @click="COMPLAINTHandle(scope.row)">{{ `投诉订单` }}</el-button>
               <template>
                  <el-button v-if="$hasPermission('biz:roomrecord:update')"
                  type="text" size="small" @click="open(scope.row)" >{{ scope.row.dictName=='订房'?'退房':''}}</el-button>
                              </template>
            <el-button type="primary" @click="childHandle(scope.row)">{{ '查看住户' }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
     <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <c-o-m-p-l-a-i-n-t v-if="COMPLAINTVisible" ref="COMPLAINT"></c-o-m-p-l-a-i-n-t>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './roomrecord-add-or-update'
import COMPLAINT from './homestaycomplaint-add-or-update'
import {addDynamicRoute} from "@/router";
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/biz/roomrecord/page',
        getDataListIsPage: true,
        exportURL: '/biz/roomrecord/export',
        deleteURL: '/biz/roomrecord',
        deleteIsBatch: true
      },
      AddRoomUserVisible: false,
      COMPLAINTVisible:false,
      dataForm: {
        roomId:'0',
        outMondy:'',
        totalMoney1:'',
        id: ''
      },
       dataForm1: {
              outMondy:'',
              roomId:'',
              id: ''
            }
    }
  },
  methods: {
  dateFormat() {
                  var date=new Date();
                  var year=date.getFullYear();
                  /* 在日期格式中，月份是从0开始的，因此要加0
                   * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
                   * */
                  var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
                  var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
                  var hours=12;
                    var hours111=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
                  var minutes='00';
                  var seconds='00';
                  // 拼接
                  if(hours111>12){
                   return year+"-"+month+"-"+(day+1)+" "+hours+":"+minutes+":"+seconds;
                                    }
                  return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
              },
   open(row) {
         var  startDate=row.startHireDate
         var endDate = this.dateFormat();
         if(startDate!==null&&endDate!==null){
           let startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
           let endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
           let dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
           this.dataForm.totalMoney1=row.totalMoney-row.dayMoney* dates}
        this.$prompt('请输入退还金额'+this.dataForm.totalMoney1, '请退还押金及剩余房费', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator:this.dataForm.totalMoney1,
          inputErrorMessage: '金额不正确'
        }).then(({ value }) => {
          if(value===this.dataForm.totalMoney1.toString()){
            this.$message({
              type: 'success',
              message: '退款成功: ' + value,
                })
                         this.dataForm1.id = row.id
                                                                  this.dataForm1.outMoney = this.dataForm.totalMoney1
                                                                   this.$http.put('/biz/roomrecord', this.dataForm1)
                                                                        .then(({ data: res }) => {
                                                                          if (res.code == 0) {
                                                                             this.getDataList()
                                                                          } })
                }else {
                        this.$message({
                          type: 'info',
                          message: '退款错误,请重新支付'
                        });
                      }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消退款'
          });
        });
      },

      // 新增 / 修改
      childHandle (row) {
        // 路由参数
        const routeParams = {
          routeName: `${this.$route.name}__${row.id}`,
          title: `${this.$route.meta.title} - ${row.roomNumber}`,
          path: 'biz/roomuser',
          params: {
            roomId: row.id
          }
        }
        // 动态路由
        addDynamicRoute(routeParams, this.$router)
      },
    // 投诉订单
    COMPLAINTHandle (row) {
      this.COMPLAINTVisible = true
      this.$nextTick(() => {
        this.$refs.COMPLAINT.dataForm.homeId1 = row.homeId1
        this.$refs.COMPLAINT.dataForm.homeId = row.homeId
        this.$refs.COMPLAINT.dataForm.location = row.location
        this.$refs.COMPLAINT.init()
      })
    },
  },
  components: {
    AddOrUpdate,
    COMPLAINT,
  },created () {
    this.dataForm.roomId = this.$route.params.id || '0'
    this.getDataList()
     this.getDataList()
      this.getDataList()
  },
}
</script>
